$(function () {
    $("#num").textbox({"onChange":function () {
           var integral =$("#gift_integral").val()
            var num =this.value
            var total = integral*num
            $("#totalCount").textbox("setValue",total)

        }})
    //选择器变量抽取
    var ex_datagrid2 = $("#ex_datagrid2");
    var ex_datagrid3 = $("#ex_datagrid3");
    var ex_datagrid1 = $("#ex_datagrid1");
    var ex_layout = $("#mylayout");
    var ex_dialog = $("#ex_dialog");
    var ex_form = $("#ex_form");
    /*页面布局*/
    ex_layout.layout({
        fit: true,
        border: true
    })


    ex_datagrid3.datagrid({
        url: '/gift/query.do',
        fit:true,
        pagination: true,
        fitColumns: true,
        singleSelect: true,
        toobar: '#ex_form',
        columns: [
            [
                {
                    field: 'sn',
                    title: '礼品编号',
                    width: 100
                },
                {
                    field: 'name',
                    title: '礼品名称',
                    width: 100
                },
                {
                    field: 'unit',
                    title: '单位',
                    width: 100
                },
                {
                    field: 'integral',
                    title: '兑换积分',
                    width: 100
                },
                {
                    field: 'number', title: '剩余量', width: 100
                },
            ]
        ],
        rownumbers: true,
    })


    //把方法绑定到对象上
    var methodObj = {
        //刷新页面
        reload: function () {
            ex_datagrid.datagrid('reload');
        },

        exportXls: function () {
            window.location.href = "/exchangeGift/exportXls.do";
        },
        //点击保存按钮
        insert: function () {
            ex_dialog.dialog('open');
            ex_dialog.dialog('setTitle', '礼品列表');
        }

    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

    //初始化对话框
    ex_dialog.dialog({
        width: 800,
        height: 450,
        buttons: "#ex_btn2",
        closed: true,
        modal: true,
        onClose: function () {
            ex_form.form('clear')
        }
    })



})
//高级查询
function query() {
    //获取查询内容
    var number = $("#number").textbox('getValue');
    $("#member_form").form('load', '/memberInfo/queryByCardNumber.do?cardNumber=' + number);
}


function selectByName() {
    var keyword = $("[name=keyword]").val();
    $("#ex_datagrid3").datagrid('reload', {
        keyword: keyword,
    })
}

function save2() {
    var row = $("#ex_datagrid3").datagrid('getSelected');
    if (!row) {
        $.messager.alert('温馨提示', '请选择一条数据', 'warning');
        return;
    }
    $("#ex_form2").form("load",row)
    $("#num").textbox("setValue","1")
    $("#totalCount").textbox("setValue",row.integral)
    cancel2()



}

function cancel2() {
    $("#ex_dialog").dialog('close');
}


$(function () {



    $("#change").click(function () {
        var row = $("#totalCount").textbox('getValue');
        var cardNumber = $("#cardNumber").textbox('getValue');
        var currentPoint = $("#currentPoint").textbox('getValue');
        var num = $("#num").textbox('getValue');
        //商品id 和商品名  兑换数量


        if (!row) {
            $.messager.alert('温馨提示', '请选择要兑换的礼品', 'warning');
            return;
        }

        if (parseFloat(currentPoint)  <parseFloat(row)) {
            $.messager.alert('温馨提示', '积分不足', 'warning');
            return;
        }
        $.messager.confirm("确认对话框", "你确定要兑换吗", function (r) {
            if (r) {

               $("#ex_form2").form("submit", {
                       url:'/gift/exchangeGift.do',
                        onSubmit: function (parm) {
                    parm.cardNumber=cardNumber
                        },
                success: function (data) {
                           //转换成json后记得接收
                    data=$.parseJSON(data)
                    if (data.success) {
                        $.messager.alert("温馨提示", "兑换成功", 'info', function () {
                            $("#ex_dialog").dialog('close');
                            $("#ex_form2").form('clear');
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'warning')
                    }
                }
            })

            }
        })
    })

});